package iterators;




import java.util.ArrayList;

import util.HtmlTextFinder;

public class VertTableIterator extends NadlanIterator {

	protected ArrayList<Integer> titleStr;//string of titles from html table
	
	//for vertical table's orientation
	protected String[] values=null;//values of current title string
	protected int colNum=-1;
	protected int colCurrent=-1;

	public VertTableIterator(String buf){this(buf,true);}
	
	protected VertTableIterator(String buf,boolean bFind)
	{
		super(buf,false);				
		titleStr=null;
		if(bFind)
			findTitle();
	}

	//	Search for title and value in html table with vertical orientation -
	//data below from title
	protected String findTitle()
	{		
		curValue=null;		
		curType=-1;		
		while(hasNext() && titleStr==null)
		{
			findTitleStr();
			if(txtFinder.indexOf("<TD")<0)
				bLast=true;
			if(titleStr!=null)
				if(hasNext())
					findVals();
				else return null;
			if(txtFinder.indexOf("<TD")<0)
				bLast=true;
		}
		
		if(titleStr==null)
			bLast=true;
		if(!hasNext())
			return null;
		
		//search for value of title found
		curType=titleStr.get(colCurrent);
		curValue=values[colCurrent];
		colCurrent++;
		if(colCurrent>=colNum)
			titleStr=null;
		return curValue;	
	}
	
	protected void findTitleStr()
	{
		int k,i;
		String sTemp;
		
		//look for one of advertisment data types
		curType=-1;
		while(hasNext() && curType<0)
		{
			if(txtFinder.indexOf("<TD")<0)
				bLast=true;
			curType=txtFinder.getType();
		}
			
		//if there is no more strings with titles return null 
		if(curType<0)
		{
			curValue=null;
			bLast=true;
			return;
		}
		
		//search for one of the data types in titleStr
		k=txtFinder.indexOf("</TR>");
		i=txtFinder.indexOf("<TABLE");
		if(i<k && i>=0)
			k=i;
						
		titleStr=new ArrayList<Integer>();
		sTemp=txtFinder.substring(txtFinder.getValPos(),k);
		i=sTemp.lastIndexOf("</TD>");
		sTemp=sTemp.substring(0,i+5);
		HtmlTextFinder titleIt=
			new HtmlTextFinder(sTemp);
		
		txtFinder.moveTo(k+5);
		int tableCol=0;
		int colType;
		while(titleIt.hasNext())
		{						
			  colType=titleIt.getType();			  
			  if(titleIt.indexOf("<TD")<0)
					titleIt.moveToEnd();

			  titleStr.add(Integer.valueOf(colType));
			  tableCol++;
		}
			
		//vertical orientid table has 4 columns, at least
		if(tableCol<4)
			titleStr=null;
		else
			colNum=tableCol;				
	}

	protected void findVals()
	{
		HtmlTextFinder titleIt;
		int k,i;
		String sTemp;
		colCurrent=0;
		
		txtFinder.moveTo(txtFinder.indexOf("<TR"));
		k=txtFinder.indexOf("</TR>");
		i=txtFinder.indexOf("<TABLE");
		if(i<k && i>=0)
			k=i;
		sTemp=txtFinder.substring(txtFinder.getPos(),k);
		i=sTemp.lastIndexOf("</TD>");
		sTemp=sTemp.substring(0,i+5);		
		titleIt=
			new HtmlTextFinder(sTemp);
		
		txtFinder.moveTo(k+5);
		values=new String[titleStr.size()];
	
		while(colCurrent<colNum)
		{
			k=titleIt.getPos();
			titleIt.moveTo(titleIt.indexOf("<TD",k));
			if(titleIt.getPos()<0)
				break;
			k=titleIt.getPos();//position after las text found
			i=titleIt.indexOf("</TD>",k);			
			sTemp=titleIt.getHtmlText();
			if(i>=0 && i<titleIt.getPos())
			{
				titleIt.moveTo(i+5);
				values[colCurrent]=" ";
			}
			else
				values[colCurrent]=sTemp;
			colCurrent++;
		}	
		colCurrent=0;
	}
	
}
